﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration; 

namespace Handrise.CDCable.Business
{
    public static class LogService
    {
        public static void SaveLog(string title, string info)
        {
            try
            {
                string writeLog = ConfigurationSettings.AppSettings["WriteLog"];

                if (writeLog.ToLower() == "true")
                {

                    StreamWriter sw = GetStreamWriter();

                    if (sw == null)
                        return;

                    sw.WriteLine(DateTime.Now.ToString() + "  " + title  + "  " + info);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch
            {
            }
 
        }

        public static string Path
        {
            get
            {
                string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                if (path.StartsWith("file:\\"))
                {
                    path = path.Remove(0, 6);
                }
                return path;
            }
        }

        private static StreamWriter GetStreamWriter()
        {
            StreamWriter sw = null;
            string fileName = Path + "\\Log\\" + DateTime.Today.ToString("yyyyMMdd") + ".log";
            if (File.Exists(fileName))
            {
                sw = File.AppendText(fileName);
            }
            else
            {
                sw = File.CreateText(fileName);
                sw.WriteLine("-----------------------------------日志文件(" + DateTime.Today.ToShortDateString() + ")-----------------------------------");
            }

            return sw;
        }
    }
}
